//-----------------------------------------------------------------------
// <copyright file="TurnoverDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class TurnoverDAOBase
    {
        #region Common methods
        public virtual Turnover CreateTurnoverFromReader(IDataReader reader)
        {
            Turnover item = new Turnover();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("TurnoverID"))) item.TurnoverID = (int)reader["TurnoverID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOder"))) item.SortOder = (int)reader["SortOder"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTurnoverFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateTurnover methods
            
        public virtual void CreateTurnover(Turnover turnover)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTurnoverInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, turnover.Title);
                database.AddInParameter(dbCommand, "@SortOder", DbType.Int32, turnover.SortOder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, turnover.Enable);
                database.AddOutParameter(dbCommand, "@TurnoverID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                turnover.TurnoverID = (int)database.GetParameterValue(dbCommand, "@TurnoverID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTurnoverException, ex);
            }
        }

        #endregion

        #region UpdateTurnover methods
        
        public virtual void UpdateTurnover(Turnover turnover)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTurnoverUpdate");            
                
                database.AddInParameter(dbCommand, "@TurnoverID", DbType.Int32, turnover.TurnoverID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, turnover.Title);
                database.AddInParameter(dbCommand, "@SortOder", DbType.Int32, turnover.SortOder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, turnover.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateTurnoverException, ex);
            }
        }
        
        #endregion

        #region DeleteTurnover methods
        public virtual void DeleteTurnover(int turnoverID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTurnoverDelete");
                
                database.AddInParameter(dbCommand, "@TurnoverID", DbType.Int32, turnoverID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteTurnoverException, ex);
            }
        }

        #endregion

        #region GetTurnover methods
        
        public virtual Turnover GetTurnover(int turnoverID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTurnoverGet");
                
                database.AddInParameter(dbCommand, "@TurnoverID", DbType.Int32, turnoverID);
                
                Turnover turnover =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        turnover = CreateTurnoverFromReader(reader);
                        reader.Close();
                    }
                }
                return turnover;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTurnoverException, ex);
            }
        }
        
        #endregion

        #region GetTurnoverList methods
        public virtual TurnoverCollection GetTurnoverList(TurnoverColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTurnoverGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                TurnoverCollection turnoverCollection = new TurnoverCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Turnover turnover = CreateTurnoverFromReader(reader);
                        turnoverCollection.Add(turnover);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return turnoverCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTurnoverListException, ex);
            }
        }
        
        public virtual TurnoverCollection GetTurnoverList(TurnoverColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetTurnoverList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetTurnoverList methods
        public virtual TurnoverCollection GetTurnoverListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTurnoverGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                TurnoverCollection turnoverCollection = new TurnoverCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Turnover turnover = CreateTurnoverFromReader(reader);
                        turnoverCollection.Add(turnover);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return turnoverCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTurnoverListException, ex);
            }
        }
        
        public virtual TurnoverCollection GetTurnoverListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetTurnoverListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}